$(document).ready(function() {
    var teNum = getUrlParms("teNum");
    //console.log(teNum);
    $.ajax({
        url: "http://localhost:8080/teacher/queryTeacher",
        type: "GET",
        async: true,
        data: "teNum=" + teNum,
        dataType: "json",
        success: function (data) {
            //console.log(data)
            //console.log($("#r2").val());

           // console.log(data.data.teacher.teName);
           // console.log(data.data.teacher.teSex);
            var teacher=data.data.teacher;
            $("#teName").val(teacher.teName);
            $("#teNum").val(teacher.teNum);
            $("#tePhone").val(teacher.tePhone);
            $("#teIdcardNum").val(teacher.teIdcardNum);
            $("#teBirthday").val(teacher.teBirthday);
            if (teacher.tePoli==0){
                $("#tePoli").val("群众");
            }else if (teacher.tePoli==1) {
                $("#tePoli").val("积极分子");
            }else if (teacher.tePoli==2) {
                $("#tePoli").val("发展对象");
            }else if (teacher.tePoli==3) {
                $("#tePoli").val("中共预备党员");
            }else{
                $("#tePoli").val("中共党员");
            }
            $("#teNation option[value='"+teacher.teNation+"']").attr("selected","selected");
            //$("#teNativeplace").attr("value",teacher.teNativeplace);
           //console.log(("#teNativeplace").val());
            $("#teNativeplace option[value='"+teacher.teNativeplace+"']").attr("selected","selected");
           // console.log($("input:radio[name='teSex'][value='" + teacher.teSex + "']").val());
           // console.log($('input:radio:first').val());
           // console.log($('input:radio:last').val());
           var r1='<label class="radio-inline">';
           var r2=' </label>';
           var r3='<input type="radio" name="radio1" value="0" id="r1"/>男';
           var r4='<input type="radio" name="radio1" value="1" id="r2"/>女';
           var r5='<input type="radio" name="radio1" value="0" id="r1" checked="checked"/>男';
           var r6='<input type="radio" name="radio1" value="1" id="r2" checked="checked"/>女';

            if (teacher.teSex==0) {
                $("#radioSex").append(r1+r5+r2+r1+r4+r2);
                // console.log($("input[name='radio1'][value='0']").val());
                // $("input[name='radio1'][value='0']").attr("checked",true);
               // $("input[name='teSex'][value='0']").prop("checked",true);

                //$("input[name='teSex'][value='1']").removeAttrs("checked");
               // $('input:radio:first').prop('checked', true);
            }else {
                $("#radioSex").append(r1+r3+r2+r1+r6+r2);
                // console.log($("input[name='radio1'][value='1']").val());
                // $("input[name='radio1'][value='1']").attr("checked",true);
               // $("input[name='teSex'][value='0']").removeAttrs("checked");
                //$('input:radio:last').prop('checked', true);
            }
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });

});

$(document).on('click', '#update-btn', function() {
    console.log($("#teBirthday").val());
    var teNum = getUrlParms("teNum");
    var $teName=$("#teName").val();
    var $tePhone=$("#tePhone").val();
    var $teIdcardNum=$("#teIdcardNum").val();
    var $teBirthday=getDate($("#teBirthday").val());
    console.log($teBirthday);
    // var $tePoli;
    // if ($("#tePoli").val("群众")){
    //     $tePoli=0;
    // }else if ($("#tePoli").val("积极分子")) {
    //     $tePoli=1;
    // }else if ($("#tePoli").val("发展对象")) {
    //     $tePoli=2;
    // }else if ($("#tePoli").val("中共预备党员")) {
    //     $tePoli=3;
    // }else{
    //     $tePoli=4;
    // }
    var $teNation=$("#teNation option:selected").val();
    var $teNativeplace=$("#teNativeplace option:selected").val();
    var $teSex= $("input[name='radio1']:checked").val();

    //封装成对象提交
    var data = {
        teNum:teNum,
        teName:$teName,
        tePhone:$tePhone,
        teIdcardNum:$teIdcardNum,
        teBirthday:$teBirthday,
        //tePoli:$tePoli,
        teNation:$teNation,
        teNativeplace:$teNativeplace,
        teSex:$teSex,
    };
    $.ajax({
        url: "http://localhost:8080/teacher/updateTeacher",
        type: "POST",
        async: false,
        data:JSON.stringify(data),
        dataType: "json",
        contentType:"application/json",
        success:function () {
            layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            layer.alert('提交成功!', function(){
                var index = parent.layer.getFrameIndex(window.name);
                setTimeout(function(){
                    parent.layer.close(index)
                }, 2000);
                parent.location.reload()
            });
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
});



function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}

function getDate(str) {
    if(str.indexOf("-") >= 0 ){
        return str;
    }else {
        var arr=str.split(" ");
        console.log(arr[1]);
        var day=arr[0];
        var month=arr[1];
        var year=arr[2];
        if(month=='January'){
            month="01";
        }else if(month=='February'){
            month="02";
        }else if(month=='March'){
            month="03";
        }else if(month=='April'){
            month="04";
        }else if(month=='May'){
            month="05";
        }else if(month=='June'){
            month="06";
        }else if(month=='July'){
            month="07";
        }else if(month=='August'){
            month="08";
        }else if(month=='September'){
            month="09";
        }else if(month=='October'){
            month="10";
        }else if(month=='November'){
            month="11";
        }else if(month=='December'){
            month="12";
        }
        return year+"-"+month+"-"+day;
    }
}